﻿using Microsoft.Data.Sqlite;
using SendMailServer.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SendMailServer
{
    internal class CodeSnip
    {
        public static ResMsg HandlerMsg(string key,string filterStr)
        {
            if (key != "GetCodeSnip") return null;
            if (filterStr == null) { filterStr = ""; }
            if (filterStr.EndsWith(",desktop"))
            {
                filterStr = filterStr.Substring(0, filterStr.Length - ",desktop".Length);
            }

            var res=GetSnips(filterStr);
            StringBuilder stringBuilder = new StringBuilder();
            for(var i = 0; i < res.Count; i++)
            {
                stringBuilder.Append(res[i].qID + "@" + res[i].catagory + "@" + res[i].codes);
                stringBuilder.Append("<%>");
            }

            return new ResMsg()
            {
                MsgCode= key,
                AppName = key,
                Content = stringBuilder.ToString(),
                IsOk = true,
                UseSelfResponse = true,
            };
        }
        public static List<CodeSnipItem> GetSnips(string filter)
        {
            StringBuilder debuggerStr = new StringBuilder();
            Microsoft.Data.Sqlite.SqliteConnection conn = null;
            Microsoft.Data.Sqlite.SqliteCommand cmd = null;
            SqliteDataReader dr = null;
            List<CodeSnipItem> res = new List<CodeSnipItem>();
            try
            {
                using (conn = DatabaseHelper.BuildConnection())
                {
                    string sql = "select  * from agcimview_codeSnap where codes like '%"+filter+"%'";
                    if (filter == "f10")
                    {
                        sql = "select  * from agcimview_codeSnap LIMIT 10";
                    }
                    cmd = new Microsoft.Data.Sqlite.SqliteCommand(sql, conn);
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        CodeSnipItem itm = new CodeSnipItem()
                        {
                            qID = (long)(dr["id"]),
                            catagory = dr["catagory"] as string,
                            codes = dr["codes"] as string
                        };

                        res.Add(itm);
                    }
                }
            }
            catch (Exception err)
            {
                res.Add(new CodeSnipItem()
                {
                    qID = -1,
                    hasError = true,
                    catagory = "错误",
                    codes = "发生了错误:" + err.Message + "," + err.StackTrace
                });
                return res;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
            return res;
        }
    }
    class CodeSnipItem
    {
        public bool hasError { get; set; } = false;
        public long qID;
        public string catagory;
        public string codes;
        public byte[] codeResImg;
    }
}
